﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using HsTec.Mobile.Green.Util;

namespace HsTec.Mobile.Green.DAL
{
    /// <summary>
    /// 数据访问类ProvinceForecast。
    /// </summary>
    public class ProvinceForecastDAL
    {
        public ProvinceForecastDAL()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sExistsWhere)
        {
            return SQLHelper.Exists("tb_ProvinceForecast", sExistsWhere);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HsTec.Mobile.Green.Model.ProvinceForecast model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_ProvinceForecast(");
            strSql.Append("Years,Value)");
            strSql.Append(" values (");
            strSql.Append("@Years,@Value)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Years", SqlDbType.Char,6),
					new SqlParameter("@Value", SqlDbType.Decimal,13)};
            parameters[0].Value = model.Years;
            parameters[1].Value = model.Value;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(HsTec.Mobile.Green.Model.ProvinceForecast model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_ProvinceForecast set ");
            strSql.Append("Years=@Years,");
            strSql.Append("Value=@Value");
            strSql.Append(" where PF_ID=@PF_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@PF_ID", SqlDbType.Int,4),
					new SqlParameter("@Years", SqlDbType.Char,6),
					new SqlParameter("@Value", SqlDbType.Decimal,13)};
            parameters[0].Value = model.PF_ID;
            parameters[1].Value = model.Years;
            parameters[2].Value = model.Value;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string PF_IDs)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_ProvinceForecast ");
            strSql.Append(" where PF_ID in ( " + PF_IDs + " )");
            return SQLHelper.ExecuteNonQuery(strSql.ToString());
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HsTec.Mobile.Green.Model.ProvinceForecast GetModel(int PF_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 PF_ID,Years,Value from tb_ProvinceForecast ");
            strSql.Append(" where PF_ID=@PF_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@PF_ID", SqlDbType.Int,4)};
            parameters[0].Value = PF_ID;

            HsTec.Mobile.Green.Model.ProvinceForecast model = new HsTec.Mobile.Green.Model.ProvinceForecast();
            DataSet ds = SQLHelper.ExecuteDataset(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["PF_ID"].ToString() != "")
                {
                    model.PF_ID = int.Parse(ds.Tables[0].Rows[0]["PF_ID"].ToString());
                }
                model.Years = ds.Tables[0].Rows[0]["Years"].ToString();
                if (ds.Tables[0].Rows[0]["Value"].ToString() != "")
                {
                    model.Value = decimal.Parse(ds.Tables[0].Rows[0]["Value"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PF_ID,Years,Value ");
            strSql.Append(" FROM tb_ProvinceForecast ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataView GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" PF_ID,Years,Value ");
            strSql.Append(" FROM tb_ProvinceForecast ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }
        #endregion  成员方法
    }
}

